package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader_Scale;
import com.smartthinking.CICPNL.home.objects.clsImagesShow;

public class Imagenes extends Activity {

	public static ArrayList<Dictionary> Actividades = null;
	public Gallery gallery1;
	public com.smartthinking.CICPNL.home.activity.Imagenes.Hoteles_Adapter myAdapter;
	public App_ImageLoader imageLoader;
	private App_ImageLoader_Scale imageLoader2;
	public int Position = 0;
	public ImageView imagec;
	private Intent intent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_imagenes);

		Actividades = clsImagesShow.getDctionary();
		
	
		
		
		
		imagec = (ImageView) Imagenes.this.findViewById(id.imageView1);
		


		CargarContenido();
		
		
		Position = 0;
		
//		imageLoader2.clearCache();
		
		CargarImagenes();
		
	}
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}
	public void voidFinish(View V) {
		finish();

	}
	
	public void abrirMenuLateral(View V) {
		
		
		
		intent = new Intent(Imagenes.this, MenuLateral.class);

		
		startActivity(intent);
	
	}
	
	
	private void CargarImagenes() {
		// TODO Auto-generated method stub
		new AsyncImagenes().execute("contenido");

	}

	private class AsyncImagenes extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			String url_img = getString(R.string.QRY_URL_IMG)
					+ Actividades.get(Position).get("foto_url").toString()
							.replace(" ", "%20");

			imagec.setTag(url_img);
			try {
				
				imageLoader2 = new App_ImageLoader_Scale(
						Imagenes.this.getApplicationContext()); 
				
//				imageLoader2.clearCache();
				imageLoader2.DisplayImage(url_img, Imagenes.this, imagec);
			


			} catch (Exception ex) {

				String s = ex.getMessage().toString();

			}

		}

		protected void abrirInformacionEvento(Dictionary d) {

			// TODO Auto-generated method stub

		}

		protected void onPreExecute() {

		}

		protected Integer doInBackground(String... urls) {

			return null;

		}

	}

	// dic = clsImagenes
	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			gallery1 = (Gallery) findViewById(R.id.gallery1);

			myAdapter = new Hoteles_Adapter(Actividades);

			gallery1.setAdapter(myAdapter);
		 
			
			DisplayMetrics metrics = new DisplayMetrics();
			Imagenes.this.getWindowManager().getDefaultDisplay()
					.getMetrics(metrics);

//			Gallery gallery = (Gallery) findViewById(R.id.gallery1);

			MarginLayoutParams mlp = (MarginLayoutParams) gallery1
					.getLayoutParams();
			mlp.setMargins(-(metrics.widthPixels / 2 + 50), mlp.topMargin,
					mlp.rightMargin, mlp.bottomMargin);
			
			
			gallery1.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					Position = position;
					
//					imageLoader2.clearCache();
					
					CargarImagenes();

				}
			});

		}

		protected void abrirInformacionEvento(Dictionary d) {

			// TODO Auto-generated method stub

		}

		protected void onPreExecute() {

		}

		protected Integer doInBackground(String... urls) {

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) Imagenes.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//
			imageLoader = new App_ImageLoader(
					Imagenes.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public ImageView icon;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {
				vi = inflater.inflate(R.layout.ui_imagenes_items, null);

				holder = new ViewHolder();

				holder.icon = (ImageView) vi.findViewById(R.id.imageView1);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			//
			String url_img = getString(R.string.QRY_URL_IMG)
					+ data.get(position).get("foto_url").toString()
							.replace(" ", "%20");

			holder.icon.setTag(url_img);
			
			try {

				imageLoader.DisplayImage(url_img, Imagenes.this, holder.icon);

			} catch (Exception ex) {

							
				String sw = ex.getMessage().toString();

			}

			return vi;

		}

	}
}